using System;
using System.Collections.Generic;
using System.Text;

namespace FetCo.Data
{
    public class Criteria
    {

        private string where;
        private string join;
        public Criteria()
        {
            where = " where 1=1";
            join = string.Empty;
        }

        public string Where
        {
            get { return where; }
            set { where = value; }
        }
        public string Join
        {
            get { return join; }
            set { join = value; }
        }
        public string RemoveSQL()
        {
            string tp = join.ToLower() + where.ToLower();
            string r1 = tp.Replace("select", "");
            string r2 = r1.Replace("drop", "");
            string r3 = r2.Replace(";", "");
            string r4 = r3.Replace("insert into", "");
            string r5 = r4.Replace("update", "");
            string r6 = r5.Replace("delete", "");
            string r7 = r6.Replace("xp_", "");
            string r8 = r7.Replace("exec", "");
            string r9 = r8.Replace("--", "");
            string r10 = r9.Replace("get", "");
            string r11 = r10.Replace("values", "").Trim().ToUpper();
            return r11;
        }
        public string Criter
        {
            get
            {
                return RemoveSQL();
            }
        }
        public void Clear()
        {
            where = " where 1=1";
        }
        public void add(object ColName, object value, string comparison, string appendType)
        {
            where = where + appendType + add(ColName, value, comparison);
        }
        public void addIsNull(object ColName, string appendType)
        {
            where = where + appendType + String.Format("{0} {1} ", ColName, Condition.ISNULL);
        }
        public void addAndIsNotNull(object ColName, string appendType)
        {
            where = where + appendType + String.Format("{0} {1} ", ColName, Condition.ISNOTNULL);
        }

        public void addBINARY(object ColName, object value, string BinnaryCondition, string comparision, object expectedvalue, string appendType)
        {
            where = where + appendType + String.Format("{0} {1} {2} {3} {4} ", ColName, BinnaryCondition, value, comparision, expectedvalue);
        }
        private string add(object ColName, object value, string comparison)
        {
            string tmp = ColName + comparison + value.ToString();
            if (value.GetType() == typeof(string))
            {
                tmp = String.Format("{0} {2} N'{1}'", ColName, value, comparison);
            }
            if (value.GetType() == typeof(string) & comparison == Condition.LIKE)
            {
                tmp = String.Format("{0} {2} N'%{1}%'", ColName, value, comparison);
            }
            if (value.GetType() == typeof(DateTime))
            {
                //tmp = String.Format("(YEAR({0}) + MONTH({0}) + Day({0})  {2}  YEAR('{1}') +  MONTH('{1}') + Day('{1}'))", ColName, value,comparison);
                tmp = String.Format("{0} {1} cast('{2}' as datetime)", ColName, comparison, value);
            }

            return tmp;

        }
        public void addShortDate(object ColName, object value, string comparison, string appendType)
        {
            where = where + appendType + addShortDate(ColName, value, comparison);
        }
        private string addShortDate(object ColName, object value, string comparison)
        {
            string tmp = ColName + comparison + value.ToString();
            if (value.GetType() == typeof(string))
            {
                tmp = String.Format("{0} {2} N'{1}'", ColName, value, comparison);
            }
            if (value.GetType() == typeof(string) & comparison == Condition.LIKE)
            {
                tmp = String.Format("{0} {2} N'%{1}%'", ColName, value, comparison);
            }
            if (value.GetType() == typeof(DateTime))
            {
                tmp = String.Format("(YEAR({0}) {2} YEAR('{1}') and MONTH({0}) {2} MONTH('{1}') and Day({0}) {2} Day('{1}'))", ColName, value, comparison);
                //tmp = String.Format("{0} {1} cast('{2}' as datetime)", ColName, comparison, value);

            }

            return tmp;

        }
        #region Join : Left Join , Inner , Right
        public void addJoin(string jointable, object LeftCol, object joinCol, string comparison, string jointype)
        {
            join = join + String.Format(" {0} {1} ON {2} {3} {4} ", jointype, jointable, LeftCol, comparison, joinCol);
        }
        #endregion
    }

}
